﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Models;

namespace Utilities
{
    public class JsonIgnoreAttribute : Attribute
    {
        public List<string> IgnoreProperties { get; set; }
        public JsonIgnoreAttribute(params string[] properties)
        {
            IgnoreProperties = new List<string>();
            IgnoreProperties.AddRange(properties);
        }
    }

    public class DataToJson<T> : JavaScriptConverter where T : class
    {
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            T theSource = obj as T;
            var ps = theSource.GetType().GetProperties();
            var c = theSource.GetType().GetCustomAttributes(typeof(JsonIgnoreAttribute), true).FirstOrDefault();

            Dictionary<string, object> OutputJson = new Dictionary<string, object>();

            foreach (var p in ps)
            {
                if (c != null)
                {
                    JsonIgnoreAttribute j = c as JsonIgnoreAttribute;
                    if (j != null && j.IgnoreProperties.Contains(p.Name))
                    {
                        continue;
                    }
                }
                OutputJson.Add(p.Name, p.GetValue(theSource, null));
            }
            return OutputJson;
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get { return new Type[] { typeof(T) }; }
        }

    }

    public class DataJsonResult<T> : JsonResult where T:class
    {
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            HttpResponseBase response = context.HttpContext.Response;
            if (!string.IsNullOrEmpty(this.ContentType))
            {
                response.ContentType = this.ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (this.ContentEncoding != null)
            {
                response.ContentEncoding = this.ContentEncoding;
            }
            if (this.Data != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                serializer.RegisterConverters(new JavaScriptConverter[] { 
                    new DataToJson<Participant>(),
                    new DataToJson<Workflow>(), 
                    new DataToJson<Department>(),
                    new DataToJson<Condition>(),
                    new DataToJson<Activity>(), 
                    new DataToJson<Field>(), 
                    new DataToJson<Employee>(),
                    new DataToJson<ActivityResult>()});
                string sJSON = serializer.Serialize(Data);
                response.Write(sJSON);
            }
        }
    }

    
}